package com.example.alarm.mvvm.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.alarm.R
import com.example.alarm.mvvm.adapter.AlarmPagerAdapter
import com.example.alarm.mvvm.bean.AlarmTypeBean
import com.google.android.material.tabs.TabLayoutMediator
import com.gsafety.libraryktx.base.BaseFragment
import com.gsafety.libraryktx.config.Constant
import com.gsafety.libraryktx.config.Constant.Companion.HAS_DEAL_CODE
import com.gsafety.libraryktx.config.Constant.Companion.IN_DEAL_CODE
import com.gsafety.libraryktx.config.Constant.Companion.NO_DEAL_CODE
import com.gsafety.libraryktx.core.RouterConstants
import com.gsafety.libraryktx.util.StatusUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.alarm_fragment_layout.*

/**预警TAB
 * @author : lj
 * @date : 2021/1/5 10:34
 */
@Route(path = RouterConstants.ALARM_FRAGMENT)
class AlarmFragment: BaseFragment() {
    var mtitles=mutableListOf<AlarmTypeBean>()
    private var mAdapter: AlarmPagerAdapter?=null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initmodel()
    }

    override fun initData() {
        mtitles.add(AlarmTypeBean("待处置(0)",NO_DEAL_CODE))
        mtitles.add(AlarmTypeBean("处置中(0)", IN_DEAL_CODE))
        mtitles.add(AlarmTypeBean("已解除(0)", HAS_DEAL_CODE))
        tablayout.isTabIndicatorFullWidth=false
        mAdapter = AlarmPagerAdapter(
            parentFragmentManager,mtitles)
        vp.adapter= mAdapter
        tablayout.setupWithViewPager(vp)
    }

    private fun setTitleFromHome(it: Array<String>) {
        tablayout.getTabAt(0)?.setText("待处置(${it[0]})")
        tablayout.getTabAt(1)?.setText("处置中(${it[1]})")
        tablayout.getTabAt(2)?.setText("已解除(${it[2]})")
    }

    private fun setTitle(it: AlarmTypeBean) {
        when(it.code){
            NO_DEAL_CODE -> tablayout.getTabAt(0)?.setText("待处置(${it.name})")
            IN_DEAL_CODE -> tablayout.getTabAt(1)?.setText("处置中(${it.name})")
            HAS_DEAL_CODE -> tablayout.getTabAt(2)?.setText("已解除(${it.name})")
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.alarm_fragment_layout

    }

    override fun initView(view: View) {
        var toolbar=view.findViewById<Toolbar>(R.id.toolbar)
        var title=view.findViewById<TextView>(R.id.title_name)
        toolbar.navigationIcon=null
        title.text=resources.getText(R.string.alarm_title)
        StatusUtils.titleBar(this,toolbar)
    }
    private fun initmodel(){
        LiveEventBus.get(Constant.EventBus_KEY_Alarm_Title,AlarmTypeBean::class.java).observe(
            this,
            Observer { it ->
                setTitle(it)
            })

        LiveEventBus.get(Constant.EventBus_KEY_Alarm_Title,Array<String>::class.java).observe(
            this,
            Observer { it ->
                setTitleFromHome(it)
            })
        LiveEventBus.get(Constant.EventBus_KEY_Alarm_Deal_Switch,String::class.java).observe(
            this,
            Observer { it ->
                tablayout.getTabAt(it.toInt())?.select()
            })
    }
}